<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>登录</title>
  <link rel="stylesheet" href="/assets/css/base.css">
  <link rel="stylesheet" href="/assets/libs/bootstrap.min.css">
  <link rel="stylesheet" href="/assets/css/login.css">
</head>

<body>
  <div class="login">
    <div class="login-item">
      <img src="/assets/img/default.png" alt="">
      <div class="form-group">

        <input type="email" class="form-control inp-email" placeholder="邮箱">
      </div>
      <div class="form-group">

        <input type="password" class="form-control inp-password" placeholder="密码">
      </div>

      <button class="btn btn-primary btn-block btn-login">登录</button>


    </div>
  </div>

  <!-- 弹出框 -->
  <div class="alert alert-danger" role="alert">
    登录失败！邮箱或者密码错误！
  </div>
  <div class="alert alert-success" role="alert">
    <p> 登陆成功！</p>
  </div>

</body>
<script src="/assets/libs/jquery-3.6.0.min.js"></script>
<script>
  $(function () {
    $('.btn-login').click(function () {
      // 获取input的值
      const email = $('.inp-email').val()
      const password = $('.inp-password').val()
      if (email && password) {
        $.ajax({
          url: 'http://192.168.2.100:3001/login',
          type: 'post',
          // 配置请求携带cookie
          xhrFields: {
            withCredentials: true
          },
          data: {
            email,
            password
          },
          success(res) {
            console.log(res);
            // 将登录成功的token值保存
            const token = res.token
            window.sessionStorage.setItem('token', token)
            $('.alert-success').attr('style', 'display:block !important')
            setTimeout(() => {
              window.location.href = 'admin.html'
            }, 500)
            // window.open('admin.html')
          },
          error(err) {
            console.log(err);
            $('.alert-danger').attr('style', 'display:block !important')
          }
        })
      } else {
        $('.alert-danger').attr('style', 'display:block !important').html('邮或密码不能为空')
      }
    })
  })
</script>


</html>